//
//  JMCouponViewController.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2019/7/19.
//  Copyright © 2019年 Lssyyy. All rights reserved.
//

#import "JMCouponViewController.h"
#import "JMCouponTableViewCell.h"
#import "JMCouponListHeadView.h"
@interface JMCouponViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *couponTableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) UIButton *dontUseCouponButton;
@end

@implementation JMCouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoutUI];
     [self initData];
}

- (void)layoutUI{
    [self.view addSubview:self.dontUseCouponButton];
    [self.view addSubview:self.couponTableView];
    self.couponTableView.sd_layout
    .leftEqualToView(self.view)
    .topEqualToView(self.view)
    .rightEqualToView(self.view)
    .bottomSpaceToView(self.dontUseCouponButton,0);
}

- (void)initData{
    [self.dataArray addObjectsFromArray:@[@[@"优惠券1",@"优惠券2优惠券2优惠券2优惠券2优惠券2优惠券2",@"优惠券3"],@[@"优惠券4",@"优惠券5",@"优惠券6"]]];
    [self.couponTableView reloadData];
}

- (void)bottomButtonMethod{
    NSLog(@"点击不使用优惠券");
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JMCouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([JMCouponTableViewCell class]) forIndexPath:indexPath];
    cell.titleStr = self.dataArray[indexPath.section][indexPath.row];
    cell.cellIndexPath = indexPath;
//    [cell configCell:self.dataArray[indexPath.section][indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView cellHeightForIndexPath:indexPath model:self.dataArray[indexPath.section][indexPath.row] keyPath:@"titleStr" cellClass:[JMCouponTableViewCell class] contentViewWidth:UI_SCREEN_WIDTH];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[JMCouponListHeadView alloc]initWithIndex:section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 69;
    }
    return 49;
}

- (UIButton *)dontUseCouponButton {
    if (!_dontUseCouponButton) {
        _dontUseCouponButton = [UIButton buttonWithType:0];
        _dontUseCouponButton.frame = CGRectMake(0, UI_SCREEN_HEIGHT -LL_NaviBarHeight -50, UI_SCREEN_WIDTH, 50);
        [_dontUseCouponButton setTitle:@"不使用优惠券" forState:0];
        [_dontUseCouponButton addTarget:self action:@selector(bottomButtonMethod) forControlEvents:UIControlEventTouchUpInside];
        _dontUseCouponButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_dontUseCouponButton setTitleColor:UIColorHex(282828) forState:UIControlStateNormal];
        UIView *sepView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 0.5)];
        sepView.backgroundColor = UIColorHex(e2e2e2);
        [_dontUseCouponButton addSubview:sepView];
    }
    return _dontUseCouponButton;
}

- (UITableView *)couponTableView{
    if (!_couponTableView) {
        _couponTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _couponTableView.backgroundColor = UIColorWithRGB16Radix(0xF3F3F3);
        _couponTableView.delegate = self;
        _couponTableView.dataSource = self;
        _couponTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_couponTableView registerClass:[JMCouponTableViewCell class] forCellReuseIdentifier:NSStringFromClass([JMCouponTableViewCell class])];
    }
    return _couponTableView;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
@end
